package com.wzh.musichome.controller;


import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.wzh.musichome.common.ConstantCode;
import com.wzh.musichome.common.ResponseTemplate;
import com.wzh.musichome.entity.dto.SongListDTO;
import com.wzh.musichome.entity.pojo.Singer;
import com.wzh.musichome.entity.pojo.Song;
import com.wzh.musichome.entity.pojo.SongList;
import com.wzh.musichome.service.SongListService;
import com.wzh.musichome.utils.EhCacheUtil;
import com.wzh.musichome.utils.FastdfsClientUtil;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;
import org.springframework.web.multipart.MultipartFile;

import java.io.File;
import java.io.IOException;
import java.util.List;

/**
 * <p>
 * 歌单 前端控制器
 * </p>
 *
 * @author wzh
 * @since 2020-12-10
 */
@RestController
@RequestMapping("/songList")
public class SongListController {
    @Autowired
    private SongListService songListService;

    @Autowired
    private FastdfsClientUtil fastdfsClientUtil;

    /**
     * 添加歌单
     */
    @PostMapping("/add")
    public Object addSongList(@RequestBody SongListDTO songListDTO) {
        //保存到歌单的对象中
        SongList songList = new SongList();
        BeanUtils.copyProperties(songListDTO, songList);
        try {
            boolean b = songListService.save(songList);
            if (b) {
                ResponseTemplate.ResultData data = ResponseTemplate.getSuccessResponse();
                data.setMsg("新增成功");
                return data;
            }
        } catch (Exception e) {
            return ResponseTemplate.getFailureResponse(ConstantCode.FAILURE_INSERT, "新增失败！！ 具体原因" + e.getMessage());
        }
        return 0;
    }

    /**
     * 修改歌单
     */
    @PostMapping("/update")
    public Object updateSongList(@RequestBody SongListDTO songListDTO) {
        SongList songList = songListService.getById(songListDTO.getId());
        BeanUtils.copyProperties(songListDTO, songList);
        try {
            boolean b = songListService.saveOrUpdate(songList);
            if (b) {
                ResponseTemplate.ResultData data = ResponseTemplate.getSuccessResponse();
                data.setMsg("更新成功");
                return data;
            }
        } catch (Exception e) {
            return ResponseTemplate.getFailureResponse(ConstantCode.FAILURE_UPDATE, "更新失败！！ 具体原因" + e.getMessage());
        }
        return 0;
    }

    @GetMapping("/delete/{id}")
    public Object deleteSongList(@PathVariable Integer id) {
        try {
            boolean b = songListService.removeById(id);
            if (b) {
                ResponseTemplate.ResultData data = ResponseTemplate.getSuccessResponse();
                data.setMsg("删除成功");
                return data;
            }
        } catch (Exception e) {
            return ResponseTemplate.getFailureResponse(ConstantCode.FAILURE_DELETE, "删除失败！！ 具体原因" + e.getMessage());
        }
        return 0;
    }

    @GetMapping("/selectByPrimaryKey/{id}")
    public Object selectByPrimaryKey(@PathVariable int id) {
        try {
            SongList byId = songListService.getById(id);
            ResponseTemplate.ResultData data = ResponseTemplate.getSuccessResponse();
            data.setMsg("查找成功");
            data.setData(byId);
            return data;
        } catch (Exception e) {
            return ResponseTemplate.getFailureResponse(ConstantCode.FAILURE_QUERY, "查找！！ 具体原因" + e.getMessage());

        }
    }

    /**
     * 查询所有歌单
     */
    @GetMapping("/allSongList")
    public Object allSongList(@RequestParam(required = false) Long current, @RequestParam(required = false) Long size) {
        try {
            ResponseTemplate.ResultData data = ResponseTemplate.getSuccessResponse();
            data.setMsg("查询成功");
            if (current != null && size != null) {
                Page<SongList> page = new Page<>();
                page.setCurrent(current);
                page.setSize(size);
                Page<SongList> list = songListService.page(page);
                if (list.getTotal() != 0) {
                    data.setData(list);
                }
            } else {
                Object cacheList = EhCacheUtil.get("local", "songList");
                if (cacheList != null) {
                    data.setData(cacheList);
                } else {
                    List<SongList> list = songListService.list();
                    data.setData(list);
                    EhCacheUtil.set("local", "songList", list);
                }
            }
            return data;
        } catch (Exception e) {
            return ResponseTemplate.getFailureResponse(ConstantCode.FAILURE_QUERY, "查询失败！！ 具体原因" + e.getMessage());
        }
    }

    /**
     * 根据标题精确查询歌单列表
     */
    @GetMapping("/songListOfTitle")
    public Object songListOfName(@RequestParam String title) {
        try {
            QueryWrapper<SongList> wrapper = new QueryWrapper<>();
            wrapper.eq("title", title);
            List<SongList> list = songListService.list(wrapper);
            ResponseTemplate.ResultData data = ResponseTemplate.getSuccessResponse();
            data.setMsg("查找成功");
            data.setData(list);
        } catch (Exception e) {
            return ResponseTemplate.getFailureResponse(ConstantCode.FAILURE_QUERY, "查找！！ 具体原因" + e.getMessage());
        }
        return 0;
    }

    /**
     * 根据风格模糊查询歌单列表
     */
    @GetMapping("/likeStyle")
    public Object likeStyle(@RequestParam String style) {
        try {
            QueryWrapper<SongList> wrapper = new QueryWrapper<>();
            wrapper.like("style", style);
            List<SongList> list = songListService.list(wrapper);
            ResponseTemplate.ResultData data = ResponseTemplate.getSuccessResponse();
            data.setMsg("查询成功");
            data.setData(list);
            return data;

        } catch (Exception e) {
            return ResponseTemplate.getFailureResponse(ConstantCode.FAILURE_QUERY, "查询失败！！ 具体原因" + e.getMessage());
        }
    }


    /**
     * 根据风格模糊查询歌单列表
     */
    @GetMapping("/likeTitle")
    public Object likeTitle(@RequestParam String title) {
        try {
            QueryWrapper<SongList> wrapper = new QueryWrapper<>();
            wrapper.like("title", title);
            List<SongList> list = songListService.list(wrapper);
            ResponseTemplate.ResultData data = ResponseTemplate.getSuccessResponse();
            data.setMsg("查询成功");
            data.setData(list);
            return data;
        } catch (Exception e) {
            return ResponseTemplate.getFailureResponse(ConstantCode.FAILURE_QUERY, "查询失败！！ 具体原因" + e.getMessage());
        }
    }

    /**
     * 更新歌单图片
     */
    @RequestMapping(value = "/updateSongListPic", method = RequestMethod.POST)
    public Object updateSongListPic(@RequestParam("file") MultipartFile avatorFile, @RequestParam("id") int id) {
        try {
            SongList byId = songListService.getById(id);
            if (!byId.getPic().equals("group1/M00/00/00/wKhJgV_lh6WATni3AAAgUovHUuE170.jpg")) {
                String[] split = byId.getPic().split("\\.");
                String sltUrl = split[0] + "_60x60." + split[1];
                fastdfsClientUtil.deleteFile(byId.getPic());
                fastdfsClientUtil.deleteFile(sltUrl);
            }
            String url = fastdfsClientUtil.upload(avatorFile);
            SongList songList = new SongList();
            songList.setId(id);
            songList.setPic(url);
            boolean flag = songListService.saveOrUpdate(songList);
            if (flag) {
                List<SongList> list = songListService.list();
                EhCacheUtil.set("local", "songList", list);
                ResponseTemplate.ResultData data = ResponseTemplate.getSuccessResponse();
                data.setMsg("歌单图片更改成功");
                return data;
            }
            return ResponseTemplate.getFailureResponse(ConstantCode.FAILURE_UPLOAD, "图片更改失败");
        } catch (IOException e) {
            return ResponseTemplate.getFailureResponse(ConstantCode.FAILURE_UPLOAD, "图片更改失败" + e.getMessage());
        } catch (Exception e) {
            return ResponseTemplate.getFailureResponse(ConstantCode.FAILURE_UPLOAD, "图片更改失败" + e.getMessage());
        }
    }


}
